﻿#include "GameScene.h"

int GetRandomTetrominoType()
{
	// 创建随机设备
	static std::random_device rd;
	// 使用 Mersenne Twister 引擎
	static std::mt19937 gen(rd());
	// 定义分布范围 (1-7)
	static std::uniform_int_distribution<int> dist(1, 7);

	return dist(gen);
}

Square* RandomNewSquare()
{
	int index = GetRandomTetrominoType();
	Square* temp = nullptr;
	switch (index)
	{
	case 1:
		temp = new Square1(BEGIN_X, BEGIN_Y - 4 * SQUARE_SIZE);
		break;
	case 2:
		temp = new Square2(BEGIN_X, BEGIN_Y - 4 * SQUARE_SIZE);
		break;
	case 3:
		temp = new Square3(BEGIN_X, BEGIN_Y - 4 * SQUARE_SIZE);
		break;
	case 4:
		temp = new Square4(BEGIN_X, BEGIN_Y - 4 * SQUARE_SIZE);
		break;
	case 5:
		temp = new Square5(BEGIN_X, BEGIN_Y - 4 * SQUARE_SIZE);
		break;
	case 6:
		temp = new Square6(BEGIN_X, BEGIN_Y - 4 * SQUARE_SIZE);
		break;
	case 7:
		temp = new Square7(BEGIN_X, BEGIN_Y - 4 * SQUARE_SIZE);
		break;
	}
	return temp;
}

int UpdateFinishedLine(bool if_draw[15][10])
{
	int finish_line_count = 0;
	for (int i = HEIGHT-1; i > -1; i--)
	{
		bool line_finish = true;
		for (int j = 0; j < WEIGHT; j++)
		{
			if (if_draw[i][j] == 0)
			{
				line_finish = false;
			}
		}

		if (line_finish)
		{
			finish_line_count++;
			for (int m = i; m > 0; m--)
			{
				for (int n = 0; n < WEIGHT; n++)
				{
					if_draw[m][n] = if_draw[m - 1][n];
				}
			}
			for (int n = 0; n < WEIGHT; n++)
			{
				if_draw[0][n] = 0;
			}
		}
	}
	return finish_line_count;
}